package com.example.ssss.ui.loginAndRegister;



import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ssss.SOSCardActivity;
import com.example.ssss.data.dao.UserDao;
import com.example.ssss.R;
import com.example.ssss.data.entity.ShareData;
import com.example.ssss.data.entity.User;

/**
 * function：连接注册页面
 */
public class Register extends AppCompatActivity {
    private static final String TAG = "mysql-SSSS-register";
    EditText userIdEditText = null;
    EditText sexEditText = null;
    EditText passwordEditText = null;
    EditText usernameEditText = null;
    EditText ageEditText = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //TODO 待指定输入框与变量关系
//        userIdEditText = findViewById(R.id.userId2);
//        sexEditText = findViewById(R.id.sex);
//        passwordEditText = findViewById(R.id.password);
//        usernameEditText = findViewById(R.id.userName);
//        ageEditText = findViewById(R.id.age);
    }


    public void register(View view){
        String userId = userIdEditText.getText().toString();
        ShareData shareData;
        shareData = (ShareData)getApplication();
        shareData.userId = userId;
        String sex = sexEditText.getText().toString();
        String password = passwordEditText.getText().toString();
        String username = usernameEditText.getText().toString();
        String age = ageEditText.getText().toString();

        User user = new User();

        user.setUserId(userId);
        user.setSex(sex);
        user.setPassword(password);
        user.setUsername(username);
        user.setAge(age);

        new Thread(() -> {

            int msg = 0;

            UserDao userDao = new UserDao();

            User uu = null;
            try {
                uu = userDao.findUser(user.getUserId());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(uu != null){
                msg = 1;
            }
            else{
                boolean flag = false;
                try {
                    flag = userDao.register(user);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(flag){
                    msg = 2;
                }
            }
            hand.sendEmptyMessage(msg);
        }).start();
    }
    @SuppressLint("HandlerLeak")
    final Handler hand = new Handler()
    {
        public void handleMessage(Message msg) {
            if(msg.what == 0) {
                Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show();
            } else if(msg.what == 1) {
                Toast.makeText(getApplicationContext(),"该账号已经存在，请换一个账号",Toast.LENGTH_LONG).show();
            } else if(msg.what == 2) {
                Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
                // TODO 跳转到填表
                Intent intent = new Intent();
                //将想要传递的数据用putExtra封装在intent中

                //setResult(RESULT_CANCELED,intent);
                intent = new Intent(Register.this, SOSCardActivity.class);
                intent.putExtra("data","register");
                startActivity(intent);
                finish();


            }
        }
    };
}
